package com.hnas.trade.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.StringUtil;
import com.hnas.trade.dao.TradeRecordDao;
import com.hnas.trade.orm.TradeRecord;
import com.hnas.trade.service.dto.DTOTradeRecord;

@Component("tradeRecordDao")
public class TradeRecordDaoImpl extends BaseDaoImpl<TradeRecord> implements TradeRecordDao{

	public TradeRecordDaoImpl() {
		super(TradeRecord.class);
	}

	@Override
	public TradeRecord saveOrUpdate(DTOTradeRecord dto) throws Exception {
		TradeRecord vo = null;
		if(dto != null){
			String id = dto.getId();
			if(StringUtil.isNotEmpty(id)){
				vo = this.find(id);
			}
			if(vo == null){//新增
				vo = new TradeRecord();
				BeanUtils.copyProperties(dto, vo);
				vo.setIsDeleted(true);
				this.persist(vo);
			}else{//修改
				BeanUtils.copyProperties(dto, vo, new String[]{"createTime","creator"});
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public TradeRecord findBySwiftNumber(String swiftNumber) {
		List<TradeRecord> list = this.findByQuery("from TradeRecord t where t.swiftNumber = ?1 and t.isDeleted="+true+" ", swiftNumber);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<TradeRecord> getList(String account_id, String order_parent_type, String order_type, int pageNo,
			int pageSize) {
		StringBuffer sb = new StringBuffer("from TradeRecord t where t.isDeleted="+true+" ");
		int j = 1;
		List<Object> paras = new ArrayList<Object>();
		if(StringUtil.isNotEmpty(account_id)){
			sb.append(" and t.accId = ?"+j++);
			paras.add(account_id);
		}
		if(StringUtil.isNotEmpty(order_parent_type)){
			sb.append(" and t.orderParentType = ?"+j++);
			paras.add(account_id);
		}
		if(StringUtil.isNotEmpty(order_type)){
			sb.append(" and t.orderType = ?"+j++);
			paras.add(account_id);
		}
		Query query = this.entityManager.createQuery(sb.toString());
		for(int i = 1; i < paras.size()+1; i++){
			query.setParameter(i, paras.get(i-1));
		}
		query.setFirstResult((pageNo-1)*pageSize);
		query.setMaxResults(pageSize);
		try{
		return query.getResultList();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<TradeRecord> getAllByDate(String account_id, String order_parent_type, String order_type, String date) {
		StringBuffer sb = new StringBuffer("from TradeRecord t where t.isDeleted="+true+" ");
		int j = 1;
		List<Object> paras = new ArrayList<Object>();
		if(StringUtil.isNotEmpty(account_id)){
			sb.append(" and t.accId = ?"+j++);
			paras.add(account_id);
		}
		if(StringUtil.isNotEmpty(order_parent_type)){
			sb.append(" and t.orderParentType = ?"+j++);
			paras.add(order_parent_type);
		}
		if(StringUtil.isNotEmpty(order_type)){
			sb.append(" and t.orderType = ?"+j++);
			paras.add(order_type);
		}
		if(StringUtil.isNotEmpty(date)){
			sb.append(" and str_to_date(t.tradeEndTime,'%Y-%m-%d') = str_to_date(?"+(j++)+",'%Y-%m-%d')");
			paras.add(date);
		}
		sb.append(" order by t.tradeEndTime desc");
		Query query = this.entityManager.createQuery(sb.toString());
		for(int i = 1; i < paras.size()+1; i++){
			query.setParameter(i, paras.get(i-1));
		}
		try{
			return query.getResultList();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<TradeRecord> findAllByDate(String startDate, String endDate) {
		String begin = startDate+" 00:00:00";
		String end = endDate+" 23:59:59";
		String sql = "select * from m_base_wxxnk_trade_record where create_time BETWEEN str_to_date('"+begin+"','%Y%m%d %H:%i:%s') AND str_to_date('"+end+"','%Y%m%d %H:%i:%s') and is_deleted="+true+"";
		Query query = this.entityManager.createNativeQuery(sql,TradeRecord.class);
		return query.getResultList();
	}

	@Override
	public List<String> findAllTradeSnoByDate(String startDate, String endDate) {
		String begin = startDate+" 00:00:00";
		String end = endDate+" 23:59:59";
		String sql = "select trade_sno from m_base_wxxnk_trade_record where trade_end_time BETWEEN str_to_date('"+begin+"','%Y%m%d %H:%i:%s') AND str_to_date('"+end+"','%Y%m%d %H:%i:%s') and is_deleted="+true+"";
		Query query = this.entityManager.createNativeQuery(sql);
		return query.getResultList();
	}

	@Override
	public TradeRecord findByTranSno(String tranSno) {
		String hql = " from TradeRecord t where t.tradeSno=?1 and t.isDeleted="+true+"";
		List<TradeRecord> list = this.findByQuery(hql, tranSno);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void deleteByTranSno(String tranSno) {
		String sql = "update m_base_wxxnk_trade_record set is_deleted="+false+" where trade_sno='"+tranSno+"'";
		Query query = this.entityManager.createNativeQuery(sql);
		query.executeUpdate();
	}

}
